//
//  MovieViewController.m
//  DSYDouban
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieCell.h"
#import "MovieModel.h"
#import "MovieCollectionViewController.h"

#import "UIImageView+WebCache.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *movieArr;

@end

@implementation MovieViewController

- (void)dealloc
{
    [_movieArr release];
    [_tableView release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"电影";
    
    // 创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[MovieCell class] forCellReuseIdentifier:@"movieCell"];
    
    // 右侧barButtonItem
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    
    
    [self dataHandle];
    
}

// 加载数据
- (void)dataHandle {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    
    NSArray *temtpArr = [NSArray arrayWithContentsOfFile:path];
    
    self.movieArr = [NSMutableArray array];
    
    for (NSDictionary *dic in temtpArr) {
        MovieModel *model = [MovieModel movieModel];
        [model setValuesForKeysWithDictionary:dic];
        
        [_movieArr addObject:model];
    }
    
//    NSLog(@"%@", _movieArr);
}


- (void)rightButtonItemAction: (UIBarButtonItem *)buttonItem {
    MovieCollectionViewController *movieCollectionVC = [[MovieCollectionViewController alloc] init];
    
    
    
    movieCollectionVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:movieCollectionVC];
//    
//    UITabBarController *tab = [[UITabBarController alloc] init];
//    tab.navigationController = nav;
    
//    [self presentViewController:nav animated:YES completion:^{
//        
//    }];
    
//    movieCollectionVC.navigationItem.leftItemsSupplementBackButton = NO;
    [self.navigationController pushViewController:movieCollectionVC animated:YES];
    
    [movieCollectionVC release];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell" forIndexPath:indexPath];
    
    MovieModel *model = _movieArr[indexPath.row];
    
    cell.titleNameLabel.text = model.title;

    // 图片网络路径名
    NSString *path = [model.images objectForKey:@"large"];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:path]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _movieArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
